// pages/stranger/stranger.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    strangerlist:[],
    id:'',
    weekarr:["星期日","星期一","星期二","星期三","星期四","星期五","星期六"],
    showflag:'',
  },
  onClose(event) {
    event.detail.instance.close();
  },
  gotoTalk(e){
    console.log(e.currentTarget.id)
    wx.navigateTo({
      url: `/pages/talk/talk?receiveId=${e.currentTarget.id}&findflag=0`,
    })
  },
  isShow(){
    let list = this.data.strangerlist;
    for(let item in list){
      if(list[item].show!=1){
        this.setData({
          showflag:0 //显示
        })
        break;
      }else{
        this.setData({
          showflag:1 //不显示
        })
      }
    }
    console.log(this.data.showflag)
  },
  // 删除对话
  delete(e){
    console.log(e.currentTarget.id)
    wx.request({
      url: `http://localhost:3000/message/deletestranger?id=${e.currentTarget.id}&receiveid=${this.data.id}`,
      method:"GET",
      success:res=>{
        console.log(res.data)
      },
      fail:err=>{
        console.error(err)
      }
    })
    wx.showToast({
      title: "删除成功",
    })
    let list = this.data.strangerlist;
    for(let item in list){
      if(list[item].sendid==e.currentTarget.id){
        list[item].show = 1;//不显示
      }
    }
    console.log(list)
    this.setData({
      strangerlist:list
    })
    this.isShow()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id:getApp().globalData.user.id
    })
  },
  // 年月日
  formatDate: function (times) {
    var date = new Date(times);
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    return year + '/' + month + '/' + day
  },
  // 年月日 时分秒
  formatDate1: function (times) {
    var date = new Date(times);
    var year = date.getFullYear(); //年份
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    var hour = function () { //获取小时
        return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    }
    var minute = function () { //获取分钟
        return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    }
    var second = function () { //获取秒数
        return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
    }
    return year + '/' + month + '/' + day + ' ' + hour() + ':' + minute() + ':' + second()
  },
  // 时分
  formatDate2: function (times) {
    var date = new Date(times);
    var hour = function () { //获取小时
        return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
    }
    var minute = function () { //获取分钟
        return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
    }
    return hour() + ':' + minute()
  },
  // 月日
  formatDate3: function (times) {
    var date = new Date(times);
    var month = date.getMonth() + 1; //月份
    var day = date.getDate(); //日
    return month + '月' + day + '日'
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let date=new Date();
    date=this.formatDate(date) //截取年月日
    date=this.formatDate1(date) //形成年月日 00：00：00
    wx.request({
      url: `http://localhost:3000/message/getstrangermeslist?id=${this.data.id}`,
      method:'GET',
      success:res=>{
        let list = res.data.list
        for(let i=0;i<list.length;i++){
          let date = this.formatDate1(list[i].time)
          list[i].date = date
          let id = list[i].sendid;
          wx.request({
            url: `http://localhost:3000/message/getreceiveid?receiveid=${id}`,
            method:"GET",
            success:res=>{
              list[i].username=res.data.user.username
              list[i].image=res.data.user.image
              // 格式化时间
              let time = list[i].time //所有
              let newtime = this.formatDate(time) //截取年月日
              newtime = this.formatDate1(newtime) //年月日 00：00：00
              let date1 = new Date(date)
              let time1 = new Date(newtime)
              let day = parseInt((date1.getTime()-time1.getTime())/(1000*60*60*24))
              if(day==0){
                list[i].time = this.formatDate2(time)
              }else if(day == 1){
                list[i].time = '昨天'
              }else if(day <7 ){
                let time2 = new Date(time)
                let week = time2.getDay()
                list[i].time = this.data.weekarr[week]
              }else{
                list[i].time = this.formatDate3(time)
              }
              console.log(list)
              this.setData({
                strangerlist:list
              })
            },
            fail:err=>{
              console.error(err)
            }
          })
        }
      },
      fail:err=>{
        console.error(err)
      }
    })
  },
})